package com.appleframework.project.demo.oss.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.appleframework.model.page.Pagination;
import com.appleframework.project.demo.common.model.ClientDetailsSo;
import com.appleframework.project.demo.common.service.ClientDetailsService;
import com.appleframework.web.bean.WrapperResponseEntity;
import com.appleframework.web.springmvc.controller.AbstractController;

@Controller
@RequestMapping("/admin/clientDetails")
public class ClientDetailsController extends AbstractController {

	private @Autowired ClientDetailsService clientDetailsService;
	
	@RequestMapping(value = "list")
	public ResponseEntity<WrapperResponseEntity> list(Long page, Long limit, ClientDetailsSo so, HttpServletRequest request) {
		Pagination pagination = new Pagination(page, limit);
		pagination = clientDetailsService.findByPage(pagination, so);
		return createResponseEntity(pagination.getList(), pagination.getTotalCount());
	}
	
	@RequestMapping(value = "list2")
	public ResponseEntity<WrapperResponseEntity> list2(Long pageIndex, Long pageSize, ClientDetailsSo so, HttpServletRequest request) {
		Pagination pagination = new Pagination(pageIndex, pageSize);
		pagination = clientDetailsService.findByPage(pagination, so);
		return createResponseEntity(pagination.getList(), pagination.getTotalCount());
	}
	
	/*@RequestMapping(value = "{id}", method = RequestMethod.GET)
	public ResponseEntity<WrapperResponseEntity> getProfile(@PathVariable("id") String id){
		ProfileEntity entity = profileMapper.selectByPrimaryKey(id);
		return new ResponseEntity<WrapperResponseEntity>(new WrapperResponseEntity(entity),HttpStatus.OK);
	}
	
	@RequestMapping(value = "options", method = RequestMethod.GET)
	public @ResponseBody List<SelectOption> getProfiles2(){
		List<SelectOption> result = new ArrayList<SelectOption>();
		List<ProfileEntity> list = profileMapper.findAllEnabledProfiles();
		for (ProfileEntity entity : list) {
			if(SecurityUtil.isSuperAdmin() || SecurityUtil.getLoginUserInfo().getGantProfiles().contains(entity.getName())){				
				result.add(new SelectOption(entity.getName(), entity.getAlias() + "("+entity.getName()+")"));
			}
		}
		return result;
	}
	
	@RequestMapping(value = "add", method = RequestMethod.POST)
	public ResponseEntity<WrapperResponseEntity> addProfile(@RequestBody ProfileEntity param){
		SecurityUtil.requireSuperAdmin();
		ProfileEntity entity = profileMapper.findByName(param.getName());
		if(entity != null)throw new JeesuiteBaseException(1002, "Profile["+param.getName()+"]已存在");
		profileMapper.insertSelective(param);
		return SUCCESS_MESSAGE;
	}
	
	@RequestMapping(value = "update", method = RequestMethod.POST)
	public ResponseEntity<WrapperResponseEntity> updateProfile(@RequestBody ProfileEntity param){
		SecurityUtil.requireSuperAdmin();
		if(param.getId() == null || param.getId() == 0){
			throw new JeesuiteBaseException(1003, "id参数缺失");
		}
		ProfileEntity entity = profileMapper.selectByPrimaryKey(param.getId());
		entity.setAlias(param.getAlias());
		entity.setName(param.getName());
		profileMapper.updateByPrimaryKey(entity);
		return SUCCESS_MESSAGE;
	}
	
	@RequestMapping(value = "delete/{id}", method = RequestMethod.GET)
	public ResponseEntity<WrapperResponseEntity> deleteProfile(@PathVariable("id") int id){
		SecurityUtil.requireSuperAdmin();
		ProfileEntity entity = profileMapper.selectByPrimaryKey(id);
		if(entity.getIsDefault())throw new JeesuiteBaseException(1003, "默认profile不能删除");
		int delete = profileMapper.deleteByPrimaryKey(id);
		return new ResponseEntity<WrapperResponseEntity>(new WrapperResponseEntity(delete > 0),HttpStatus.OK);
	}*/
	
	
}
